//
//  HMMainTableViewController.swift
//  sinaweibo
//
//  Created by WGP on 16/4/8.
//  Copyright © 2016年 WGP. All rights reserved.
//

import UIKit

class HMMainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
//        self.tabBar.tintColor = UIColor.orangeColor()
        
//        get only 只读属性,不能直接赋值,使用KVC
        let mainTabBar = HMTabBar()
        
        mainTabBar.addClosuer =
        {
//            model控制器,UIView不能Model出控制器
            let composeVC = HMComposeViewController()
            let nav = UINavigationController(rootViewController: composeVC)
            self.presentViewController(nav, animated: true, completion: nil)
        }
        
        setValue(mainTabBar, forKey: "tabBar")
        
        setUpChildViewControllers()
    }

    private func setUpChildViewControllers()
    {
        addChildVC(HMHomeTableViewController(), imageName: "tabbar_home", title: "首页")
        addChildVC(HMMessageTableViewController(), imageName: "tabbar_message_center", title: "消息")
        addChildVC(HMDisconverTableViewController(), imageName: "tabbar_discover", title: "发现")
        addChildVC(HMProfileTableViewController(), imageName: "tabbar_profile", title: "我")
    }
    
    
    private func addChildVC(viewController:UIViewController,imageName:String,title:String)
    {
        let navVC = UINavigationController(rootViewController: viewController)
        viewController.tabBarItem.image = UIImage(named: imageName)
        viewController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
//        解决方法:
//        1.tintColor只适用于标题和图片颜色一致的情况下
//        2.图片的渲染模式
        viewController.title = title
        viewController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)
        addChildViewController(navVC)
        
        
    }

}
